# SI: Figure 1
# =======================================================================================

# keep only observations with mixed gender reviewership and at least one female reviewer
to_plot <- manuscript_data %>% 
  filter(n_fem_rev >0 & reviewership == "mixed") %>%
  filter(is.na(first_decision) == F)

# make variable on number of female reviewers and aggregate review score
to_plot <- to_plot %>%
  mutate(n_fem_rev = ifelse(n_fem_rev == 1, "1", ifelse(n_fem_rev == 2, "2", ">=3"))) %>%
  group_by(gender_type3, n_fem_rev) %>%
  summarise(rev_score_opt_avg = weighted.mean(rev_score_opt, w = n_complete_reviewers)
            ,non_reject_avg = mean(non_reject)
            , n = n()
            , se = weighted_se(rev_score_opt, w = n_complete_reviewers))

# make factor variables for plotting
to_plot <- to_plot %>% mutate(n_fem_rev = factor(n_fem_rev, levels = c("1", "2", ">=3"))
                              , gender_type3 = factor(gender_type3, levels = c("Male", "Mixed Team", "Female")))

# calculate sample mean
mean_rev_scrore <- weighted.mean(manuscript_data$rev_score_opt, w = manuscript_data$n_complete_reviewers, na.rm = T)

# make variable for labelling
to_plot <- to_plot %>% mutate(n = as.character(n)
                              , n = str_pad(n, width = 4, side = "right", pad = " "))

# plot 
p <- ggplot(to_plot, aes(x = n_fem_rev , y = rev_score_opt_avg, fill = gender_type3)) + 
  geom_col(position = "dodge") + 
  geom_errorbar(aes(x = n_fem_rev, ymin = rev_score_opt_avg-1.96*se, ymax = rev_score_opt_avg+1.96*se)
                , position = position_dodge(width=0.9), width=0.2, colour="black") + 
  scale_fill_viridis_d() + 
  labs(x = "Number of female reviewers", y = "Review score", fill = "Authorship"
       , caption = "Mixed Reviewership only.\nWeighted by total number of reviewers.") + 
  theme_minimal() + 
  geom_hline(yintercept = mean_rev_scrore, lty = 2) + 
  theme(legend.position = "bottom") +
  geom_text(data = filter(to_plot, gender_type3 == "Female"), aes(label = paste("N=", n, "\n", sep = ""), y = .005)
            , hjust = -1.3, size = 6, col = "black", position = position_dodge(width=0.9)) + 
  geom_text(data = filter(to_plot, gender_type3 == "Male"), aes(label = paste("N=", n, "\n", sep = ""), y = .005)
            , hjust = 2.1, size = 6, col = "white", position = position_dodge(width=0.9)) + 
  geom_text(data = filter(to_plot, gender_type3 == "Mixed Team"), aes(label = paste("N=", n, "\n", sep = ""), y = .005)
            , size = 6, col = "black", position = position_dodge(width=0.9)) + 
  theme(text = element_text(size = 25))

# save

# as PDF
ggsave(plot = p, filename = here("02_output", "02_figures", "figure_s1.pdf") 
       , width = 14, height = 10)

# as TIFF
ggsave(plot = p, here("02_output", "02_figures", "figure_s1.tiff") 
       , width = 14, height = 10, dpi = 300, device = "tiff")
